<?php
class LotteryAction extends HomeAction {

    public function index(){

        //自定义分享
        $jsapi_ticket = $this->get_jsapi_ticket();
        $noncestr = $this->GetRandStr(10);
        $timestamp = time();
        $url = C('site_url').__SELF__;
        $string = 'jsapi_ticket='.$jsapi_ticket.'&noncestr='.$noncestr.'&timestamp='.$timestamp.'&url='.$url;
        $signature = sha1($string);
        $this->assign('jsapi_ticket',$jsapi_ticket);
        $this->assign('noncestr',$noncestr);
        $this->assign('timestamp',$timestamp);
        $this->assign('signature',$signature);

        $this->display();       
    }

    #抽奖
    public function play(){
        $cfg = M('LotteryConfig')->find(); //活动配置
        $cfg['name'] = unserialize($cfg['name']);
        $cfg['value'] = unserialize($cfg['value']);
        $cfg['probability'] = unserialize($cfg['probability']);

        $prize_arr = array( 
            '0' => array('id'=>1,'min'=>302,'max'=>358,'prize'=>$cfg['name'][0],'value'=>$cfg['value'][0],'v'=>$cfg['probability'][0]), 
            '1' => array('id'=>2,'min'=>2,'max'=>58,'prize'=>$cfg['name'][1],'value'=>$cfg['value'][1],'v'=>$cfg['probability'][1]), 
            '2' => array('id'=>3,'min'=>62,'max'=>122,'prize'=>$cfg['name'][2],'value'=>$cfg['value'][2],'v'=>$cfg['probability'][2]), 
            '3' => array('id'=>4,'min'=>122,'max'=>178,'prize'=>$cfg['name'][3],'value'=>$cfg['value'][3],'v'=>$cfg['probability'][3]), 
            '4' => array('id'=>5,'min'=>182,'max'=>238,'prize'=>$cfg['name'][4],'value'=>$cfg['value'][4],'v'=>$cfg['probability'][4]), 
            '5' => array('id'=>6,'min'=>242,'max'=>298,'prize'=>$cfg['name'][5],'value'=>$cfg['value'][5],'v'=>$cfg['probability'][5]), 
        ); 


        foreach ($prize_arr as $key => $val) { 
            $arr[$val['id']] = $val['v']; 
        }            
        $rid = $this->getRand($arr,I('get.f')); //根据概率获取奖项id    
        $res = $prize_arr[$rid-1]; //中奖项

         
        $min = $res['min']; 
        $max = $res['max']; 
        $result['angle'] = mt_rand($min,$max); //随机生成一个角度 

        $result['prize'] = $res['prize']; 
        $result['id'] = $res['id'];          
        $result['value'] = $res['value'];          
        echo json_encode($result); 
    } 

    //保存中奖信息
    public function save(){

        $lid = I('get.lid');
        $cfg = M('LotteryConfig')->find(); //活动配置
        $cfg['name'] = unserialize($cfg['name']);
        $cfg['value'] = unserialize($cfg['value']);
        $cfg['probability'] = unserialize($cfg['probability']);
        $cfg['pkName'] = unserialize($cfg['pkName']);


        $obj = M('PointDetail');
             
        //保存中奖信息
        $data['memberID'] = $this->_user['memberID'];
        $data['type'] = 1;
        $data['point'] = 10;
        $data['event'] = $cfg['name'][$lid-1].$cfg['value'][$lid-1];
        $data['val'] = $lid;
        if ($lid==5) { //送积分
            $data['status'] = 1;
        }
        $data['createTime'] = time();  
        $data['updateTime'] = time();  
        $list = $obj->add($data);
        if ($list) {
            //如果不是五等奖10个积分
            if ($lid!=5) {
                //减去积分
                unset($map);
                $map['id'] = $this->_user['memberID'];        
                M('Member')->where($map)->setDec('point',10);
            }
            if ($lid<5){//填写中奖信息
                $url = U('Lottery/submit',array('id'=>$list));
            }else{
                $url = U('Lottery/index');
            }     
            echo $url;
        }else{
            echo 'error';
        }
    }

    public function getRand($proArr) { 
        $result = '';  
        //概率数组的总概率精度 
        $proSum = array_sum($proArr);  
        //概率数组循环 
        foreach ($proArr as $key => $proCur) {            
            $randNum = mt_rand(1, $proSum); 
            if ($randNum <= $proCur) { 
                $result = $key; 
                break; 
            } else { 
                $proSum -= $proCur; 
            }
        }

        unset ($proArr);
        return $result; 
    } 

    public function history(){
        $map['memberID']=$this->_user['memberID'];
        $obj = M('PointDetail');
        $count = $obj->where($map)->count();
        import("@.ORG.WapPage");
        $page = new Page($count, 10);
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();
    }

    //填写中奖信息
    public function submit(){
        $id = I('get.id');
        $map['id']=$id;
        $map['memberID']=$this->_user['memberID'];
        $list = M('PointDetail')->where($map)->find();
        if ($list) {
            unset($map);
            $map['memberID'] = $this->_user['memberID'];
            $address = M('Address')->where($map)->order('def desc , id desc')->find();
            $this->assign('address',$address);

            $this->assign('list',$list);
            $this->display();
        }else{
            $this->error('信息不存在');
        }        
    }

    public function address(){
        $obj = M('PointDetail');
        if ($data = $obj->create()) {
            if ($list = $obj->save($data)) {
                $state = 'SUCCESS';
                $message = '操作成功';
                $url = U('Member/index');                                
            } else {
                $state = '操作失败';
            }
        } else {
            $state = $obj->getError();
        }
        echo $this->echo_json_str($state,$message,$url); 
    }
}